﻿using System;
using AccelerateSensor.Model.Gateway;
using AccelerateSensor.Service.Tools;
using System.Collections.Generic;
using System.Linq;
using AccelerateSensor.Model.Node;

namespace AccelerateSensor.Service.DbProxy.SQLite
{
    // ReSharper disable once IdentifierTypo
    internal class SqliteDbProxy
    {
        public void Init()
        {
            // ReSharper disable once IdentifierTypo
            var sqliteHelper = new SqliteHelper();
            if (!sqliteHelper.InitDatabase())
            {
                LogHelper.AddErrorLog($"InitSQLite database failed: {sqliteHelper.LastErrorMsg}");
            }
        }

        #region 获取网关信息

        public List<GatewayInfo> GetGatewayInfoList()
        {
            var gatewayInfoList = new List<GatewayInfo>();

            try
            {
                var bllGateway = new Bll.Gateway();
                var gatewayList = bllGateway.GetAllModels();

                var bllGatewayCommonAttribute = new Bll.GatewayCommonAttribute();
                var gatewayCommonAttributeList = bllGatewayCommonAttribute.GetAllModels();

                var bllGatewayPrivateAttribute = new Bll.GatewayPrivateAttribute();
                var gatewayPrivateAttributeList = bllGatewayPrivateAttribute.GetAllModels();

                foreach (var gateway in gatewayList)
                {
                    var commonAttributeInfo = gatewayCommonAttributeList.FirstOrDefault(
                        t => t.GatewayUuid.Equals(gateway.Uuid));
                    var privateAttributeInfo = gatewayPrivateAttributeList.FirstOrDefault(
                        t => t.GatewayUuid.Equals(gateway.Uuid));
                    var commonInfo = DataConverter.GetGatewayCommonAttributeInfo(commonAttributeInfo);
                    var privateInfo = DataConverter.GetGatewayPrivateAttributeInfo(privateAttributeInfo);
                    gatewayInfoList.Add(new GatewayInfo()
                    {
                        Uuid = gateway.Uuid,
                        Type = gateway.Type,
                        CommonAttributeInfo = commonInfo,
                        PrivateAttributeInfo = privateInfo,
                        UpdateTime = gateway.UpdateTime,
                        LocalUpdateTime = gateway.LocalUpdateTime
                    });
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogHelper.AddErrorLog(e.Message);
            }

            return gatewayInfoList;
        }

        #endregion

        #region 更新网关信息

        public void UpdateGatewayInfo(GatewayInfo gatewayInfo)
        {
            var gatewayUuid = gatewayInfo?.Uuid;
            if (string.IsNullOrWhiteSpace(gatewayUuid))
            {
                return;
            }

            AddOrUpdateGateway(gatewayInfo, gatewayUuid);

            if (gatewayUuid != "0")
            {
                AddOrUpdateGatewayCommonAttribute(gatewayInfo.CommonAttributeInfo, gatewayUuid);
                AddOrUpdateGatewayPrivateAttribute(gatewayInfo.PrivateAttributeInfo, gatewayUuid);
            }
        }

        public void UpdateGatewayInfo(string nodeUuid, DateTime updateTime)
        {
            try
            {
                var bllNode = new Bll.Node();
                var nodeList = bllNode.GetAllModels();
                var node = nodeList.FirstOrDefault(t => nodeUuid.Equals(t.Uuid));
                if (node == null)
                {
                    return;
                }

                var bllGateway = new Bll.Gateway();
                var gatewayList = bllGateway.GetAllModels();
                var gateway = gatewayList.FirstOrDefault(t => t.Uuid == node.GatewayUuid);
                if (gateway == null)
                {
                    return;
                }

                gateway.UpdateTime = updateTime;
                gateway.LocalUpdateTime = DateTime.Now;
                bllGateway.UpdateByUuid(gateway);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogHelper.AddErrorLog(e.Message);
            }
        }

        private void AddOrUpdateGateway(GatewayInfo info, string gatewayUuid)
        {
            try
            {
                var bll = new Bll.Gateway();
                var gatewayList = bll.GetAllModels();
                if (gatewayList.FirstOrDefault(t => gatewayUuid.Equals(t.Uuid)) == null)
                {
                    AddGatewayInfo(info);
                }
                else
                {
                    var gateway = DataConverter.GetGateway(info);
                    bll.UpdateByUuid(gateway);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogHelper.AddErrorLog(e.Message);
            }
        }

        private void AddOrUpdateGatewayCommonAttribute(GatewayCommonAttributeInfo info, string gatewayUuid)
        {
            if (info == null)
            {
                return;
            }

            try
            {
                var bll = new Bll.GatewayCommonAttribute();
                var attributeList = bll.GetAllModels();
                if (attributeList.FirstOrDefault(t => gatewayUuid.Equals(t.GatewayUuid)) == null)
                {
                    AddGatewayCommonAttributeInfo(gatewayUuid, info);
                }
                else
                {
                    var attribute = DataConverter.GetGatewayCommonAttribute(info);
                    bll.UpdateByGatewayUuid(attribute);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogHelper.AddErrorLog(e.Message);
            }
        }

        private void AddOrUpdateGatewayPrivateAttribute(GatewayPrivateAttributeInfo info, string gatewayUuid)
        {
            if (info == null)
            {
                return;
            }

            try
            {
                var bll = new Bll.GatewayPrivateAttribute();
                var attributeList = bll.GetAllModels();
                if (attributeList.FirstOrDefault(t => gatewayUuid.Equals(t.GatewayUuid)) == null)
                {
                    AddGatewayPrivateAttributeInfo(gatewayUuid, info);
                }
                else
                {
                    var attribute = DataConverter.GetGatewayPrivateAttribute(info);
                    bll.UpdateByGatewayUuid(attribute);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogHelper.AddErrorLog(e.Message);
            }
        }

        #endregion

        #region 添加网关信息

        private void AddGatewayInfo(GatewayInfo gatewayInfo)
        {
            try
            {
                var gateway = DataConverter.GetGateway(gatewayInfo);
                var bll = new Bll.Gateway();
                bll.Add(gateway);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogHelper.AddErrorLog(e.Message);
            }
        }

        private void AddGatewayCommonAttributeInfo(string gatewayUuid, GatewayCommonAttributeInfo info)
        {
            try
            {
                var attribute = DataConverter.GetGatewayCommonAttribute(info, gatewayUuid);
                var bll = new Bll.GatewayCommonAttribute();
                bll.Add(attribute);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogHelper.AddErrorLog(e.Message);
            }
        }

        private void AddGatewayPrivateAttributeInfo(string gatewayUuid, GatewayPrivateAttributeInfo info)
        {
            try
            {
                var attribute = DataConverter.GetGatewayPrivateAttribute(info, gatewayUuid);
                var bll = new Bll.GatewayPrivateAttribute();
                bll.Add(attribute);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogHelper.AddErrorLog(e.Message);
            }
        }

        #endregion

        #region 获取节点信息

        public List<NodeInfo> GetNodeList(string gatewayUuid)
        {
            var nodeInfoList = new List<NodeInfo>();

            try
            {
                var bllNode = new Bll.Node();
                var nodeList = bllNode.GetAllModels();

                var bllNodeCommonAttribute = new Bll.NodeCommonAttribute();
                var nodeCommonAttributeList = bllNodeCommonAttribute.GetAllModels();

                var bllNodePrivateAttribute = new Bll.NodePrivateAttribute();
                var nodePrivateAttributeList = bllNodePrivateAttribute.GetAllModels();

                foreach (var node in nodeList)
                {
                    var commonAttributeInfo = nodeCommonAttributeList.FirstOrDefault(
                        t => t.NodeUuid.Equals(node.Uuid));
                    var privateAttributeInfo = nodePrivateAttributeList.FirstOrDefault(
                        t => t.NodeUuid.Equals(node.Uuid));
                    var commonInfo = DataConverter.GetNodeCommonAttributeInfo(commonAttributeInfo);
                    var privateInfo = DataConverter.GetNodePrivateAttributeInfo(privateAttributeInfo);
                    nodeInfoList.Add(new NodeInfo()
                    {
                        Uuid = node.Uuid,
                        GatewayUuid = gatewayUuid,
                        Type = node.Type,
                        CommonAttributeInfo = commonInfo,
                        PrivateAttributeInfo = privateInfo,
                        UpdateTime = node.UpdateTime,
                        LocalUpdateTime = node.LocalUpdateTime
                    });
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogHelper.AddErrorLog(e.Message);
            }

            return nodeInfoList;
        }

        #endregion

        #region 更新节点信息

        public void UpdateNodeInfo(NodeInfo nodeInfo)
        {
            var nodeUuid = nodeInfo?.Uuid;
            if (string.IsNullOrWhiteSpace(nodeUuid))
            {
                return;
            }

            AddOrUpdateNode(nodeInfo, nodeUuid);
            AddOrUpdateNodeCommonAttribute(nodeInfo.CommonAttributeInfo, nodeUuid);
            AddOrUpdateNodePrivateAttribute(nodeInfo.PrivateAttributeInfo, nodeUuid);
        }

        public void UpdateNodeInfo(string nodeUuid, DateTime updateTime)
        {
            try
            {
                var bll = new Bll.Node();
                var nodeList = bll.GetAllModels();
                var nodeInfo = nodeList.FirstOrDefault(t => nodeUuid.Equals(t.Uuid));
                if (nodeInfo == null)
                {
                    return;
                }
                
                nodeInfo.UpdateTime = updateTime;
                nodeInfo.LocalUpdateTime = DateTime.Now;
                bll.UpdateByUuid(nodeInfo);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogHelper.AddErrorLog(e.Message);
            }
        }

        private void AddOrUpdateNode(NodeInfo info, string nodeUuid)
        {
            try
            {
                var bll = new Bll.Node();
                var nodeList = bll.GetAllModels();
                if (nodeList.FirstOrDefault(t => nodeUuid.Equals(t.Uuid)) == null)
                {
                    AddNodeInfo(info);
                }
                else
                {
                    var node = DataConverter.GetNode(info);
                    bll.UpdateByUuid(node);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogHelper.AddErrorLog(e.Message);
            }
        }

        private void AddOrUpdateNodeCommonAttribute(NodeCommonAttributeInfo info, string nodeUuid)
        {
            if (info == null)
            {
                return;
            }

            try
            {
                var bll = new Bll.NodeCommonAttribute();
                var attributeList = bll.GetAllModels();
                if (attributeList.FirstOrDefault(t => nodeUuid.Equals(t.NodeUuid)) == null)
                {
                    AddNodeCommonAttributeInfo(nodeUuid, info);
                }
                else
                {
                    var attribute = DataConverter.GetNodeCommonAttribute(info);
                    bll.UpdateByNodeUuid(attribute);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogHelper.AddErrorLog(e.Message);
            }
        }

        private void AddOrUpdateNodePrivateAttribute(NodePrivateAttributeInfo info, string nodeUuid)
        {
            if (info == null)
            {
                return;
            }

            try
            {
                var bll = new Bll.NodePrivateAttribute();
                var attributeList = bll.GetAllModels();
                if (attributeList.FirstOrDefault(t => nodeUuid.Equals(t.NodeUuid)) == null)
                {
                    AddNodePrivateAttributeInfo(nodeUuid, info);
                }
                else
                {
                    var attribute = DataConverter.GetNodePrivateAttribute(info);
                    bll.UpdateByNodeUuid(attribute);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogHelper.AddErrorLog(e.Message);
            }
        }

        #endregion

        #region 添加节点信息

        private void AddNodeInfo(NodeInfo nodeInfo)
        {
            try
            {
                var attribute = DataConverter.GetNode(nodeInfo);
                var bll = new Bll.Node();
                bll.Add(attribute);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogHelper.AddErrorLog(e.Message);
            }
        }

        private void AddNodeCommonAttributeInfo(string nodeUuid, NodeCommonAttributeInfo info)
        {
            try
            {
                var attribute = DataConverter.GetNodeCommonAttribute(info, nodeUuid);
                var bll = new Bll.NodeCommonAttribute();
                bll.Add(attribute);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogHelper.AddErrorLog(e.Message);
            }
        }

        private void AddNodePrivateAttributeInfo(string nodeUuid, NodePrivateAttributeInfo info)
        {
            try
            {
                var attribute = DataConverter.GetNodePrivateAttribute(info, nodeUuid);
                var bll = new Bll.NodePrivateAttribute();
                bll.Add(attribute);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogHelper.AddErrorLog(e.Message);
            }
        }

        #endregion
    }
}
